package com.zl.freight.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.zhy.autolayout.AutoLayoutActivity;
import com.zl.freight.mode.BaseJPushEntity;
import com.zl.freight.mode.BaseUserEntity;
import com.zl.freight.mode.LoginAfter;
import com.zl.freight.mode.MoneySettingsMode;
import com.zl.freight.utils.API;
import com.zl.freight.utils.SoapCallback;
import com.zl.freight.utils.SoapUtils;
import com.zl.freight.utils.SpUtils;
import com.zl.zlibrary.utils.GsonUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/3.
 */

public class BaseActivity extends AutoLayoutActivity {

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    protected AppCompatActivity mActivity;
    protected ProgressDialog baseDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        baseDialog = new ProgressDialog(mActivity);
        baseDialog.setMessage("加载中...");
    }

    protected void showToast(String message) {
        Toast.makeText(mActivity, message, Toast.LENGTH_LONG).show();
    }

    /**
     * 找到控件
     *
     * @param viewId
     * @param <T>
     * @return
     */
    protected <T extends View> T getView(Integer viewId) {
        return (T) findViewById(viewId);
    }

    /**
     * 显示dialog 点击外部会消失
     *
     * @param Message
     */
    protected void showDialog(String Message) {
        baseDialog.setCanceledOnTouchOutside(true);
        baseDialog.setMessage(Message);
        baseDialog.show();
    }

    /**
     * 显示dialog 点击外部不会消失
     *
     * @param Message
     */
    protected void showNotTouchDialog(String Message) {
        baseDialog.setCanceledOnTouchOutside(false);
        baseDialog.setMessage(Message);
        baseDialog.show();
    }

    protected void showDialog() {
        baseDialog.setCanceledOnTouchOutside(true);
        baseDialog.show();
    }

    protected void showNotTouchDialog() {
        baseDialog.setCanceledOnTouchOutside(false);
        baseDialog.show();
    }

    //储存配置信息如：临时发货要多少钱等
    protected void saveData() {
        Map<String, String> params = new HashMap<>();
        SoapUtils.Post(mActivity, API.BaseSet, params, new SoapCallback() {
            @Override
            public void onError(String error) {

            }

            @Override
            public void onSuccess(String data) {
                JSONArray array = null;
                try {
                    array = new JSONArray(data);

                    LoginAfter loginAfter = new LoginAfter();
                    for (int i = 0; i < array.length(); i++) {
                        MoneySettingsMode mode = GsonUtils.fromJson(array.optString(i), MoneySettingsMode.class);
                        switch (mode.getCodeName()) {
                            case "实名认证奖励积分":
                                loginAfter.setRealname(mode.getCodeValue());
                                break;
                            case "公司认证奖励积分":
                                loginAfter.setCompany(mode.getCodeValue());
                                break;
                            case "车辆认证奖励积分":
                                loginAfter.setCar(mode.getCodeValue());
                                break;
                            case "推介人认证奖励积分":
                                loginAfter.setReferral(mode.getCodeValue());
                                break;
                            case "司机临时发货费用":
                                loginAfter.setInfoMoney(mode.getCodeValue());
                                break;
                            case "发布1条消息费用":
                                loginAfter.setDriverSend(mode.getCodeValue());
                                break;
                            case "查车收费标准":
                                loginAfter.setLookCarLocation(mode.getCodeValue());
                                break;
                            case "查询司机收费标准":
                                loginAfter.setRealDriver(mode.getCodeValue());
                                break;
                        }
                    }
                    if (!TextUtils.isEmpty(data)) {
                        SpUtils.setLoginAfter(mActivity, loginAfter);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    protected void hideDialog() {
        baseDialog.dismiss();
    }

    protected void upLoadRegId() {
        BaseUserEntity userData = SpUtils.getUserData(mActivity);
        Map<String, String> params = new HashMap<>();
        BaseJPushEntity entity = new BaseJPushEntity();
        entity.setUserId(userData.getId());
        entity.setRegistrationId(SpUtils.getRegId(mActivity));
        entity.setMobile(userData.getUserName());
        params.put("baseJPushEntityJson", GsonUtils.toJson(entity));
        SoapUtils.Post(API.InsertJpush, params, new SoapCallback() {
            @Override
            public void onError(String error) {
                Log.e("error", "");
            }

            @Override
            public void onSuccess(String data) {
                Log.e("data", data);
            }
        });
    }

    /**
     * 更新积分
     *
     * @param type  0增1减
     * @param jiFen
     */
    protected void upDateMoney(int type, int jiFen) {
        BaseUserEntity userData = SpUtils.getUserData(mActivity);
        int i = Integer.parseInt(userData.getIntegral());
        if (type == 0) {
            userData.setIntegral((i + jiFen) + "");
        } else if (type == 1) {
            if (jiFen <= i) {
                userData.setIntegral((i - jiFen) + "");
            } else {
                userData.setIntegral(i + "");
                showToast("发生异常");
            }
        }

        SpUtils.setUserData(mActivity, userData);
    }

    protected void hideKeyboard(View view){
        //隐藏软键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // 获取软键盘的显示状态
        boolean isOpen = imm.isActive();
        if (isOpen) {
            // 隐藏软键盘
            imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 检查是否有该权限
     *
     * @param permission
     * @return
     */
    public boolean isPermission(String permission) {
        return (ContextCompat.checkSelfPermission(mActivity, permission)
                == PackageManager.PERMISSION_GRANTED);
    }

    /**
     * 申请应用授权
     *
     * @param permissions 权限数组（同一组权限的）
     */
    protected void applyPermission(String[] permissions, final PermissionListener listener) {
        applyPermission(permissions, 0x100, listener);
    }

    /**
     * 申请应用授权
     *
     * @param permissions 权限数组（同一组权限的）
     */
    protected void applyPermission(String[] permissions, int code, final PermissionListener listener) {
        //是否需要动态申请权限
        if (Build.VERSION.SDK_INT >= 23) {
            AndPermission.with(this)
                    .requestCode(code)
                    .permission(permissions)
                    .callback(new PermissionListener() {
                        @Override
                        public void onSucceed(int requestCode, @android.support.annotation.NonNull List<String> grantPermissions) {
                            if (listener != null) {
                                listener.onSucceed(requestCode, grantPermissions);
                            }
                        }

                        @Override
                        public void onFailed(int requestCode, @android.support.annotation.NonNull List<String> deniedPermissions) {
                            if (listener != null) {
                                listener.onFailed(requestCode, deniedPermissions);
                            }
                        }
                    }).start();
        }
    }


}
